<template>
  <div class="banner">
    <el-col :span="24" class="action-bar">
      <!-- <span style="color:#888888;">栏目管理:</span> -->
			<el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
    </el-col>

    <!--列表-->
		<el-table :data="menu" highlight-current-row class="el-table-style">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60" sortable>
			</el-table-column>
			<el-table-column label="icon" width="150" sortable>
				<template slot-scope="scope">
					<img :src="scope.row.img_url" width="50" height="50"/>
				</template>
			</el-table-column>
			<el-table-column prop="title" label="标题" width="100" sortable>
			</el-table-column>
      <el-table-column prop="link_url" label="链接" width="100" sortable>
		  <template slot-scope="scope">
					<div>
						<el-button type="primary"
						v-show="scope.row.link_url"
						v-clipboard:copy="scope.row.link_url"
						v-clipboard:success="onCopy"
						size="mini"
						v-clipboard:error="onError">复制链接</el-button>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="appid" label="appid" width="100" sortable>
			</el-table-column>
      <el-table-column prop="sort" label="排序" width="80" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button type="danger" class="danger-btn" size="mini" @click="delFeaturesMenu(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-col :span="24" class="footer-bar">
			<!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
			<div class="page-code">
				<el-pagination
					background
					layout="prev, pager, next"
					:total="pageTotal"
					:page-size="limit"
					@current-change="reqFeaturesMenu"
				>
				</el-pagination>
			</div>
    </el-col>

    <!-- 新增 -->
		<el-dialog title="新增栏目" :visible.sync="addFormVisible" width="40%">
			<el-form :model="addForm" label-width="80px">
				<el-form-item label="icon">
					<el-upload
						class="avatar-uploader"
						action="https://tengxiansh.dindong.cn/api/file/upload-img"
						name="photo"
						:show-file-list="false"
						:on-success="addUploadSuccess"
					>
						<img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
        <el-form-item label="标题">
					<el-input v-model="addForm.title" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="链接">
					<el-input v-model="addForm.link_url" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="appid">
					<el-input v-model="addForm.appid" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="排序">
					<el-input v-model="addForm.sort" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addCancelBtn">取消</el-button>
				<el-button @click.native="setFeaturesMenu" type="primary">提交</el-button>
			</div>
		</el-dialog>
    <!-- 编辑 -->
		<!-- <el-dialog title="编辑轮播图" :visible.sync="editFormVisible" width="40%">
			<el-form :model="addForm" label-width="80px">
				<el-form-item label="图片">
					<el-upload
						class="avatar-uploader"
						action="https://jsonplaceholder.typicode.com/posts/"
						:show-file-list="false"
						:on-success="handleAvatarSuccess"
						:before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="排序" prop="sort">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary">提交</el-button>
			</div>
		</el-dialog> -->
  </div>
</template>

<script>
export default {
  data(){
    return{
      addFormVisible: false,//新增界面是否显示
      editFormVisible: false,//编辑界面是否显示
      menu:[],
			imageUrl:'',
			limit:16,
			pageTotal: 0,
      //新增界面数据
			addForm: {
				imgId:'',
				link_url: '',
				imgUrl: '',
				title: '',
				appid: '',
				sort: '',
				status: 1
			},
    }
	},
	mounted(){
		this.reqFeaturesMenu();
	},
  methods:{
	  onCopy(e){
			this.$message({
				type:'success',
				message:'复制成功'
			})
		},
		onError(erro){
			this.$message({
				type:'error',
				message:'复制失败'
			})
		},
		//新增取消按钮
		addCancelBtn(){
			this.addForm.imgId = "";
			this.addForm.imgUrl = "";
      this.addForm.link_url = "";
      this.addForm.title = "";
      this.addForm.appid = "";
      this.addForm.sort = "";
      this.addForm.status = 1;
			this.addFormVisible = false;
		},
		//新增按钮重置表单
		addBtnReset(){
			this.addForm.imgId = "";
			this.addForm.imgUrl = "";
      this.addForm.link_url = "";
      this.addForm.title = "";
      this.addForm.appid = "";
      this.addForm.sort = "";
      this.addForm.status = 1;
			this.addFormVisible = true;
		},
		//获取栏目列表
		async reqFeaturesMenu(page){
			const params = {
				limit: this.limit,
				page:page || 1,
			};
			const res = await this.$api.getFeaturesMenu(params);
      if(res.data.level=='success'){
        let list = res.data.data;
				this.menu = list;
				this.pageTotal = res.data.page_info.total;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//新增提交按钮
		async setFeaturesMenu(){
			const params = {
				img_id: this.addForm.imgId,
				link_url: this.addForm.link_url,
				title: this.addForm.title,
				appid:this.addForm.appid,
				sort:this.addForm.sort,
				status:this.addForm.status,
			};
			const res = await this.$api.setFeaturesMenu(params);
      if(res.data.level=='success'){
        this.$message({
          message: res.data.message,
          type: 'success'
				});
				this.reqFeaturesMenu();
				this.addFormVisible = false;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//删除栏目
		async delFeaturesMenu(id){
			const params = {
        id:id,
			}
			//删除
			const judge= this.$confirm('删除该权限?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(async() => {
					let res = await this.$api.delFeaturesMenu(params)
					if(res.data.level=='success'){
						this.reqFeaturesMenu();
						this.$message({
							type: 'success',
							message: res.data.message
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.message
						});
					}
				}).catch(() => {
			});
		},
    addUploadSuccess(res, file) {
			if(res.data){
				this.addForm.imgUrl = res.data.img_url;
				this.addForm.imgId = res.data.id;
			}
		},
	}
}
</script>
<style lang='scss' scoped>
  .banner{
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
      margin: 10px 0px;
      box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
  }
</style>

<style>
	.el-pagination.is-background .el-pager li,
	.el-pagination.is-background .btn-prev,
	.el-pagination.is-background .btn-next
	{
		background-color: #fff;
		border: 1px solid #f1f1f1;
		margin: 0;
	}
  .member .el-dialog__body{overflow: auto;}
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
